package com.dao.impl;

import com.dao.BaseDao;
import com.dao.Export_infoDao;
import com.pojo.Export_info;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Export_infoDaoImpl implements Export_infoDao {

    private Connection connection = BaseDao.getConn();
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    /***
     * 获取每页出库列表
     * @param pageIndex
     * @param pageSize
     * @param search
     * @return
     */
    @Override
    public List<Export_info> findPageExportList(int pageIndex, int pageSize, String search) {
        String sql = "select * from export_info where 1=1";
        if (!Objects.equals(search, "")) {
            sql += " and customer like '%" + search + "%'";
        }
        sql += " and status = 0";
        sql += " limit " + pageSize + " offset " + pageSize * (pageIndex - 1);
        ArrayList<Export_info> exportInfos = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Export_info exportInfo = new Export_info();
                setExportAttr(exportInfo, resultSet);
                exportInfos.add(exportInfo);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return exportInfos;
    }

    /***
     * 获取出库总数
     * @return
     */
    @Override
    public int findAllExportCount(String search) {
        String sql = "select count(1) from export_info where status = 0";
        if (!Objects.equals(search, "")) {
            sql += " and customer like '%" + search + "%'";
        }
        return queryCount(sql);
    }

    /***
     * 获取单个出库信息、编辑页面
     * @param id
     * @return
     */
    @Override
    public Export_info findExportById(int id) {
        String sql = "select * from export_info where export_no = ?;";
        Export_info exportInfo = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                exportInfo = new Export_info();
                setExportAttr(exportInfo, resultSet);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return exportInfo;
    }

    /***
     * 编辑更新出库
     * @param exportInfo
     * @return
     */
    @Override
    public int updateExport(Export_info exportInfo) {
        String sql = "update export_info set medicine_id = ?, customer = ?, export_date = ?, quantity = ?, unit_price = ?, total_price = ?, status = ? where export_no = ?;";
        return BaseDao.ExcuteUpdate(sql, exportInfo.getMedicineInfo().getMEDICINE_ID(), exportInfo.getCustomer(), exportInfo.getExportDate(), exportInfo.getQuantity(),
                exportInfo.getUnitPrice(), exportInfo.getTotalPrice(), exportInfo.getStatus(), exportInfo.getExportNo());
    }

    /***
     * 添加出库
     * @param exportInfo
     * @return
     */
    @Override
    public int addExport(Export_info exportInfo) {
        String sql = "insert into export_info values(null, ?, ?, ?, ?, ?, ?, default);";
        return BaseDao.ExcuteUpdate(sql, exportInfo.getMedicineInfo().getMEDICINE_ID(), exportInfo.getCustomer(), exportInfo.getExportDate(), exportInfo.getQuantity(),
                exportInfo.getUnitPrice(), exportInfo.getTotalPrice());
    }

    /***
     * 删除出库 status 0是有效 1是无效
     * @param id
     * @return
     */
    @Override
    public int delExport(int id) {
        String sql = "update export_info set status = 1 where export_no = ?;";
        return BaseDao.ExcuteUpdate(sql, id);
    }

    @Override
    public int delExports(String ids) {
        String sql = "update export_info set status = 1 where export_no in (";
        sql += ids;
        sql = sql.substring(0, sql.length() - 1);
        sql += ");";
        return BaseDao.ExcuteUpdate(sql);
    }

    /***
     * 公共方法设置出库对象的属性
     * @param exportInfo
     * @param resultSet
     */
    @Override
    public void setExportAttr(Export_info exportInfo, ResultSet resultSet) throws SQLException {
        exportInfo.setExportNo(resultSet.getInt(1));
        exportInfo.setMedicineInfo(new Medicine_infoDaoImpl().queryMedicineInfoById(resultSet.getInt(2)));
        exportInfo.setCustomer(resultSet.getString(3));
        exportInfo.setExportDate(resultSet.getString(4));
        exportInfo.setQuantity(resultSet.getInt(5));
        exportInfo.setUnitPrice(resultSet.getDouble(6));
        exportInfo.setTotalPrice(resultSet.getDouble(7));
        exportInfo.setStatus(resultSet.getInt(8));
    }

    /***
     * 公共方法查询总数
     * @param sql
     * @return
     */
    public int queryCount(String sql) {
        int allExportCount = 0;
        Connection connection = BaseDao.getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                allExportCount = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);

        } finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return allExportCount;
    }

    /**
     * 根据药品id查询该药品出库总数
     * @param id
     * @return
     */
    public int queryExportQuantityByMedicineId(int id) {
        String sql = "select sum(QUANTITY) from export_info where MEDICINE_ID = ?";
        int imsum = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                imsum = resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return imsum;
    }
}
